package com.ymttest.database.mongo;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.TimeTypeAdapter;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.ymt.core.tool.FormatUtil;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

/**
 * 购物车
 * 
 * @author zhouyi
 *
 */
public class ShoppingCartMongoWapper {
	MongoDBDriver mdd;

	public ShoppingCartMongoWapper() throws Exception {
		/*mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip"), Integer.valueOf(EnvSetup
				.getSystemUrl("set5_mongoport")), "Ymt_ShoppingCart");*/
		
		mdd = YmtMongoDriverFactroy.getInstance_YmtShoppingCartDBdriverNew("YmtShoppingCart");

		//mdd.selectCollection("YmtShoppingCart");
		
		}
	
	/**
	 * 根据条件查询一条购物车
	 * 
	 * @param part
	 * @return ShopCartMongoBean
	 * @throws Exception
	 */
	public ShopCartMongoBean getOneShoppingCartInfo(String part) throws Exception {
		String re = null;
		ShopCartMongoBean scmb = null;
		ArrayList<String> list = mdd.selectPartLimit(part, 1);
		try {
			re = list.get(0);
			scmb = (ShopCartMongoBean) FormatUtil.GSONToObject(re, ShopCartMongoBean.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return scmb;
	}
	
	/**
	 * 根据条件查询一条购物车
	 * 
	 * @param part
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public Map<String,Object> getOneShoppingCartInfoForMap(String part) throws Exception {

		ArrayList<Map> list = mdd.selectPartLimitToMap(part, 1);
	    
	    return list.size() > 0 ? list.get(0) : null;
	}

	/**
	 * 根据条件查询一组购物车数据列表
	 * 
	 * @param part
	 * @param limit
	 *            返回list最大size，为0时没有限制
	 * @return ShopCartMongoBean
	 * @throws Exception
	 */
	public ArrayList<ShopCartMongoBean> getShoppingCartInfoList(String part, int limit) throws Exception {
		ArrayList<ShopCartMongoBean> list = new ArrayList<ShopCartMongoBean>();
		List<String> re = null;
		if (limit > 0) {
			re = mdd.selectPartLimit(part, limit);

		} else if (limit == 0) {
			re = mdd.selectPart(part);
		} else {
			throw new Exception("getShoppingCartInfoList limit 参数错误");
		}
		for (String sre : re) {
			ShopCartMongoBean scmb = (ShopCartMongoBean) FormatUtil.JSONToObject(sre, ShopCartMongoBean.class);
			list.add(scmb);
		}
		return list;
	}
	
	/**
	 * 根据条件查询一组购物车数据列表
	 * 
	 * @param part
	 * @param limit
	 *            返回list最大size，为0时没有限制
	 * @return ShopCartMongoBean
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public ArrayList<YmtShoppingCartBean> getYmtShoppingCartInfoList(String part, int limit) throws Exception {
		ArrayList<YmtShoppingCartBean> list = new ArrayList<YmtShoppingCartBean>();
		ArrayList<Map> mapre = null;
		if (limit > 0) {
			mapre = mdd.selectPartLimitToMap(part, "", limit, "{\"lastmodifiedtime\":-1}");
			//mapre = mdd.selectPartLimitToMap(part, limit);
		} else {
			throw new Exception("getYmtShoppingCartInfoList limit 参数错误");
		}
		for (Map sre : mapre) {
			JSONObject object = new JSONObject(sre);
			Gson gson = new GsonBuilder().setDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").create();
			YmtShoppingCartBean scmb = gson.fromJson(object.toString(), YmtShoppingCartBean.class);
			list.add(scmb);
		}

		return list;
	}
	
	/**
	 *查询某用户购物车中状态为正常的商品信息
	 *按修改时间倒叙排列
	 * **/
	public List<Map> selectShoppingInfo(int userid,int limit) throws Exception{
		ArrayList<Map> notInfo = null;
		
		notInfo = mdd.selectPartLimitToMap("{\"userid\":"+userid+",\"status\":0}","{\"_id\":0}",limit,"{\"lastmodifiedtime\":-1}");

		return notInfo;
		
	}

	/**
	 * 随机获取一条购物车记录
	 * 
	 * @return ShopCartMongoBean
	 * @throws Exception
	 */
	public ShopCartMongoBean getRandomShoppingCartInfo() throws Exception {
		String re = null;
		ShopCartMongoBean scmb = null;
		// 获取默认排序10到1000条记录中的随机一条
		ArrayList<String> list = mdd.selectPartLimit("{}", 1000);
		try {
			int ran = new Random().nextInt(list.size());
			if (ran < 10) {
				ran = ran + 10;
			}
			re = list.get(ran);
			scmb = (ShopCartMongoBean) FormatUtil.JSONToObject(re, ShopCartMongoBean.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return scmb;
	}
	
	

	/**
	 * 根据条件查询记录数量
	 * 
	 * @param part
	 *            条件
	 * @return 数量
	 * @throws Exception
	 */
	public int getShoppingCartCount(String part) throws Exception {
		int count = mdd.selectCount(part);
		return count;
	}
	protected void close() throws Throwable {
		mdd.close();
	}
	
	public static void main(String[] args) throws Exception{
		ShoppingCartMongoWapper wapper = new ShoppingCartMongoWapper();
		//String _id = wapper.getOneShoppingCartInfoForMap("{\"catalogid\":\"f2494810-079f-48f4-b9b0-68c96959001c\"}").get("_id").toString();
		String _id = FormatUtil.beanToGSONString(wapper.getYmtShoppingCartInfoList("{\"userid\":20226821}", 100).get(0));
		System.out.println(_id);
	}
	
}
